﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;

namespace kinect8.Toolkit
{
    public class SwipeGestureDetector : GestureDetector
    {

        const float SwipeMinimalLength = 0.3f;
        const float SwipeMaximalHeight = 0.3f;
        const float SwipeMaximalDepth = 0.2f;
        const int SwipeMininalDuration = 50;
        const int SwipeMaximalDuration = 2000;

        double startX;
        double startY;
        double startZ;
        DateTime startTime;
        JointType trackHand;
        bool trackStarted;

        DateTime lastoccured;
        double eventspan = 800;
		double aa = 100;

        public SwipeGestureDetector(JointType hand)
        {
            trackHand = hand;
            trackStarted = false;
            Enable = true;
            lastoccured = DateTime.Now;
        }
        public override bool TrackGesture(Skeleton skeleton)
        {
            if ((DateTime.Now - lastoccured).TotalMilliseconds < eventspan)
            {
                trackStarted = false;

                return false;
            }


            if (skeleton == null)
            {
                trackStarted = false;
                return false;
            }


            if (skeleton.Joints[trackHand].TrackingState != JointTrackingState.Tracked)
            {
                trackStarted = false;
                return false;
            }
            if (!trackStarted)
            {
                startX = skeleton.Joints[trackHand].Position.X;
                startY = skeleton.Joints[trackHand].Position.Y;
                startZ = skeleton.Joints[trackHand].Position.Z;
                startTime = DateTime.Now;
                trackStarted = true;
                return false;
            }
            if (trackStarted)
            {
                double TotalMilliseconds = (DateTime.Now - startTime).TotalMilliseconds;
                if (TotalMilliseconds > SwipeMaximalDuration)
                {
                    trackStarted = false;
                    return false;
                }
                if (TotalMilliseconds < SwipeMininalDuration)
                {
                    return false;
                }
                if (Math.Abs(skeleton.Joints[trackHand].Position.Y - startY) > SwipeMaximalHeight ||
                    Math.Abs(skeleton.Joints[trackHand].Position.Z - startZ) > SwipeMaximalDepth)
                {
                    trackStarted = false;
                    return false;
                }
                if (skeleton.Joints[trackHand].Position.X - startX > SwipeMinimalLength)
                {
                    //swipe to right
                    RaiseGestureDetected(new GestureEventArgs("SwipeToRight"));
                    lastoccured = DateTime.Now;

                    
                    trackStarted = false;
                    return true;
                }
                if (skeleton.Joints[trackHand].Position.X - startX < -SwipeMinimalLength)
                {
                    //swipe to left
                    RaiseGestureDetected(new GestureEventArgs("SwipeToLeft"));
                    lastoccured = DateTime.Now;

                    
                    trackStarted = false;
                    return true;
                }

            }

            return false;

        }
    }
}
